//
//  ScrollNewsView.swift
//  JTYP
//
//  Created by Groot on 2017/12/5.
//  Copyright © 2017年 inspur. All rights reserved.
//

import Alamofire

class ScrollNewsView: UIView, SGAdvertScrollViewDelegate {
    
    static let height: CGFloat = 40
    // 滚动新闻条数
    static let rows: Int = 5
    
    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var scrollNewsView: SGAdvertScrollView!
    var notices: [Notice]?
    
    override func awakeFromNib() {
        scrollNewsView.delegate = self
        scrollNewsView.titleFont = UIFont.systemFont(ofSize: 15)
        requestNews()
    }
    
    func requestNews() {
        let params = ["type": NoticeType.message.rawValue,
                      "start": 1,
                      "size": ScrollNewsView.rows,
                      "order": "desc",
                      "sort": "publishtime"] as [String : Any]
        
        NetWorkUtil.shareInstance.postForObject(objectType: ListResponse<Notice>.self, urlString: Server.noticeList, parameters: params, finishCallBack:({ [unowned self](obj, error) in
            
            guard let result = obj else {
                return
            }
            self.notices = result.data
            self.reload()
        }))
    }
    
    private func reload() {
        scrollNewsView.titles = self.notices?.flatMap({ (o) -> String? in
            return o.title
        })
    }
    
    func advertScrollView(_ advertScrollView: SGAdvertScrollView!, didSelectedItemAt index: Int) {
        
    }
}
